package com.haojiu.controller;

import com.haojiu.pojo.User;
import com.haojiu.service.UserService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * @author:我叫浩九
 * @date：2021/8/28 15:47
 * create in RuGao JiangSu
 */
@Controller
public class DownController {
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download() throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //给要下载的文件起一个名字

        String downName = new String("速度与激情9.jpg".getBytes("utf-8"), "iso8859-1");
        headers.setContentDispositionFormData("attachment", downName);
        //G盘中的0.jpg是待下载的图片。这里讲待下载的图片放在G盘，也可以放在项目目录下
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File("F:/img/19122501.jpg")),
                headers, HttpStatus.CREATED);
    }
    @RequestMapping("/userDetails")
    public ModelAndView getDetails(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        User user = userService.queryUserByName(username);
        mv.addObject("user",user);
        mv.setViewName("user/person");
        return mv;
    }

}
